package com.jelly.io.nio.lock;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileLock;
import java.util.concurrent.TimeUnit;

/**
 * 文件加锁
 * 
 * @author Jelly
 * 
 */
public class FileLocking {
	public static void main(String[] args) throws IOException, InterruptedException {
		FileOutputStream fos = new FileOutputStream("file.txt");
		FileLock fl= fos.getChannel().tryLock();
		System.out.println(fl.isShared());
		if(fl!=null){
			System.out.println("Locked File");
			TimeUnit.MILLISECONDS.sleep(1000);
			fl.release();
			System.out.println("Release Lock");
		}
		System.out.println(fl.isShared());
	}
}
